﻿using CleanArchitecture.Core.Configuration;
using CleanArchitecture.Core.Interfaces;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace CleanArchitecture.Infrastructure.StartupConfig
{
    /// <summary>
    /// Represents object for the configuring common features and middleware on application startup
    /// </summary>
    public class CommonStartup : INowaStartup
    {
        /// <summary>
        /// Add and configure any of the middleware
        /// </summary>
        /// <param name="services">Collection of service descriptors</param>
        /// <param name="configuration">Configuration of the application</param>
        public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            //compression
            services.AddResponseCompression();

            //add options feature
            services.AddOptions();

            //add memory cache  
            services.AddMemoryCache();

            //add Easy caching
            //official website: https://github.com/dotnetcore/EasyCaching
            services.AddEasyCaching();

            //add distributed memory cache
            services.AddDistributedMemoryCache();

            //add HTTP sesion state feature
            services.AddHttpSession();

            //add anti-forgery
            services.AddAntiForgery();

            //add default HTTP clients
            services.AddCustomHttpClients();
        }

        /// <summary>
        /// Configure the using of added middleware
        /// </summary>
        /// <param name="application">Builder for configuring an application's request pipeline</param>
        public void Configure(IApplicationBuilder application)
        {
            //use response compression
            application.UseResponseCompression();

            //use static files feature
            application.UseCustomStaticFiles();

            //check whether requested page is keep alive page
            application.UseKeepAlive();

            //use HTTP session
            application.UseSession();

            //use request localization
            application.UseRequestLocalization();

            //set request culture
            application.UseCulture();

        }

        /// <summary>
        /// Gets order of this startup configuration implementation
        /// </summary>
        public int Order
        {
            //common services should be loaded after error handlers
            get { return StartupOrderConfig.CommonStartupOrder; }
        }
    }
}